VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "VBEModuleReader"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Implements IVBModuleReader

Private mLines() As String
Private mCodeLength As Long
Private mIndex As Long
Private mCode As CodeModule

Private Property Get IVBModuleReader_AtEndOfStream() As Boolean
    IVBModuleReader_AtEndOfStream = (mIndex >= UBound(mLines))
End Property

Private Sub IVBModuleReader_ConstructFrom(rawMaterial As Variant)

' expecting rawMaterial to be a VBE CodeModule
    
    If TypeName(rawMaterial) <> "CodeModule" Then
        Debug.Print "not a CodeModule"
    End If
    
Dim i As Long
    
    Set mCode = rawMaterial
    
    Debug.Print "Loading " & mCode.Parent.Name
    
    ReDim mLines(0 To mCode.CountOfLines - 1)
    
    For i = 0 To mCode.CountOfLines - 1
        mLines(i) = mCode.Lines(i + 1, 1)
    Next
    
    mIndex = 0
    
End Sub

Private Property Get IVBModuleReader_FileName() As String
    IVBModuleReader_FileName = mCode.Parent.Name
End Property

Private Property Get IVBModuleReader_FileSize() As Long
    IVBModuleReader_FileSize = mCodeLength
End Property

Private Property Get IVBModuleReader_ModuleType() As vbModuleType
    Select Case mCode.Parent.Type
    Case vbext_ct_StdModule
        IVBModuleReader_ModuleType = vbmtmodule
    Case vbext_ct_ClassModule
        IVBModuleReader_ModuleType = vbmtclass
    Case vbext_ct_MSForm
        IVBModuleReader_ModuleType = vbmtForm
    End Select
End Property

Private Function IVBModuleReader_NextVBLine() As VBLine
    
Dim newLine As VBLine
    
    Set newLine = New VBLine
    
    newLine.AddText NextLine
    Do While (newLine.HasContinuation And Not IVBModuleReader_AtEndOfStream)
        newLine.AddText NextLine
    Loop
    
    Set IVBModuleReader_NextVBLine = newLine

End Function

Private Function NextLine() As String
    
    Do While Trim(NextLine) = "" And Not IVBModuleReader_AtEndOfStream
        mIndex = mIndex + 1
        NextLine = CStr(mLines(mIndex))
    Loop
    
'    Debug.Print Format(mIndex, "0000") & " " & NextLine
    
    mCodeLength = mCodeLength + Len(NextLine)

End Function
